package com.ohuang.myapp.fragment;


import com.ohuang.baseapp.eventbus.EventBus;
import com.ohuang.baseapp.livedata.LiveDataBus;
import com.ohuang.baseapp.livedata.Observer;
import com.ohuang.baseapp.utils.ToastUtils;
import com.ohuang.baseapp.view.refresh.OhRefrsh.OhRefreshLayout;
import com.ohuang.baseapp.view.refresh.OhRefrsh.OnRefreshListener;
import com.ohuang.baseapp.view.refresh.OhRefrsh.Refresh;
import com.ohuang.myapp.ResourceTable;
import com.ohuang.myapp.bean.TieBa;
import com.ohuang.myapp.provider.ListProvider2;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

public class TwoFragment extends LogFragment {

    public static TwoFragment newInstance() {
        TwoFragment frag = new TwoFragment();
        return frag;
    }
    @Override
    protected int getXmlId() {
        return ResourceTable.Layout_fg_two;
    }

    OhRefreshLayout ohRefreshLayout;
    ListContainer listContainer;
    ListProvider2 listProvider;

    @Override
    protected void initComponent() {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        ohRefreshLayout = (OhRefreshLayout) findComponentById(ResourceTable.Id_pullTo);
        List<TieBa> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add(new TieBa("这是第" + i + "条内容"));
        }
        LiveDataBus.get().with("1",String.class).addObserverForSticky(this, new Observer<String>() {
            @Override
            public void onchange(String s) {
                ToastUtils.showToastDialog(getContext(),s);
            }
        });
        listProvider = new ListProvider2(list, getContext());
        listContainer.setItemProvider(listProvider);
        ohRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(Refresh refresh) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        getContext().getUITaskDispatcher().syncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                list.add(0, new TieBa("new message"));
                                EventBus.get().post(list);
                                listProvider.notifyDataChanged();
                                refresh.onRelease();
                            }
                        });
                    }
                }).start();
            }


            @Override
            public void onBottomRefresh(Refresh refresh) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        getContext().getUITaskDispatcher().syncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                list.add(new TieBa("new more message"));
                                EventBus.get().post(list);
                                listProvider.notifyDataChanged();
                                refresh.refreshComplete();

                            }
                        });
                    }
                }).start();

            }
        });
    }
}

